package com.cloud.wms.ui.data.activity;

import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;

import androidx.appcompat.widget.AppCompatSpinner;

import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.ScanText;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.ui.data.contract.CheckInventoryContact;
import com.cloud.wms.ui.data.entity.InventoryEntity;
import com.cloud.wms.ui.data.presenter.CheckInventoryPresenter;

import java.util.List;

import butterknife.BindView;

/**
 * Created by FC on 2021/3/10.
 * Desc：查询库存
 */
public class CheckInventoryActivity extends BaseBarListActivity<InventoryEntity.CommodityEntity, CheckInventoryPresenter> implements CheckInventoryContact.View, AdapterView.OnItemSelectedListener {
    @BindView(R.id.spinner)
    AppCompatSpinner spinner;
    @BindView(R.id.scan_text)
    ScanText scanText;
    @BindView(R.id.btn_select)
    ImageButton btnSelect;
    private int checkInventoryType;

    @Override
    protected String barTitle() {
        return "库存查询";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_check_inventory;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initScanText(scanText);
        spinner.setOnItemSelectedListener(this);
        btnSelect.setOnClickListener(v -> {
            if (TextUtils.isEmpty(scanText.getText())) {
                info("关键字不能为空");
                return;
            }
            mPresenter.getInventoryMsg(scanText.getText(), checkInventoryType);
            scanText.setText("");
        });
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_check_inventory;
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, InventoryEntity.CommodityEntity item) {
        helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                .setText(R.id.tv_bar_code, item.getBarCode())
                .setText(R.id.tv_pos_code, item.getPosCode())
                .setText(R.id.tv_coding, item.getCommodityCode())
                .setText(R.id.tv_style_code, item.getSizeCode())
                .setText(R.id.tv_color, item.getColorCode())
                .setText(R.id.tv_amount, String.valueOf(item.getCurrentQty()))
                .setText(R.id.tv_occupy_amount, String.valueOf(item.getReservedQty()))
                .setText(R.id.tv_production_date, item.getProductionDate())
                .setText(R.id.tv_expire_date, item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate())
                .setText(R.id.tv_member_name, item.getMemberName())
                .setGone(R.id.tv_style, TextUtils.isEmpty(item.getSizeCode()))
                .setGone(R.id.tv_style_code, TextUtils.isEmpty(item.getSizeCode()))
                .setGone(R.id.tv_color_title, TextUtils.isEmpty(item.getColorCode()))
                .setGone(R.id.tv_color, TextUtils.isEmpty(item.getColorCode()));
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case CheckInventoryPresenter.CHECK_INVENTORY_SUCCESS:
                InventoryEntity inventoryEntity = (InventoryEntity) message.obj;
                mAdapter.setNewInstance(inventoryEntity.getItems());
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        String[] selectType = getResources().getStringArray(R.array.check_inventory_data);
        scanText.setHint(selectType[position]);
        checkInventoryType = position + 1;
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
